package com.asp.aspchain.tsttransaction.service.impl;

import com.alibaba.fastjson.JSON;
import com.asp.aspchain.core.dao.AppBaseMapper;
import com.asp.aspchain.core.dao.MgrAccountMapper;
import com.asp.aspchain.core.dao.TocAccountMapper;
import com.asp.aspchain.core.dao.TransactionMapper;
import com.asp.aspchain.core.ethereum.EthService;
import com.asp.aspchain.core.model.AppBaseModel;
import com.asp.aspchain.core.model.MgrAccountModel;
import com.asp.aspchain.core.model.TocAccountModel;
import com.asp.aspchain.core.model.TransactionModel;
import com.asp.aspchain.core.util.AjaxResult;
import com.asp.aspchain.core.util.DateUtil;
import com.asp.aspchain.tsttransaction.request.TransactionQueryTradeReq;
import com.asp.aspchain.tsttransaction.request.TransactionWithdrawReq;
import com.asp.aspchain.tsttransaction.service.EoaQueryTradeService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.web3j.protocol.core.methods.request.Transaction;

import java.io.IOException;
import java.math.BigInteger;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhongzifeng
 */
@Service
public class EoaQueryTradeServiceImpl implements EoaQueryTradeService {
	private final static Logger logger = Logger.getLogger(EoaQueryTradeServiceImpl.class);

    @Value("${tst.accountkey}")
    private String accountKey;

	@Autowired
	private EthService ethService;

	@Autowired
	private MgrAccountMapper mgrAccountMapper;

	@Autowired
	private TocAccountMapper tocAccountMapper;

	@Autowired
	private AppBaseMapper appBaseMapper;

	@Autowired
    private TransactionMapper transactionMapper;


	/**
	 * 交易查询接口
	 * @param json transaction query param
	 * @return
	 */
	@Override
	public AjaxResult eoaQueryTrade(String json){
        AjaxResult result = new AjaxResult();
        // 转换参数至实体
        TransactionQueryTradeReq req = JSON.parseObject(json, TransactionQueryTradeReq.class);
        MgrAccountModel selectMgrAccount = new MgrAccountModel();
        // 校验充值账户是否属于指定appmark
        selectMgrAccount.setAccoutkey(accountKey);
        MgrAccountModel mgrAccountModel = mgrAccountMapper.selectOne(selectMgrAccount);
        String appmark = req.getAppmark();
        AppBaseModel selectAppBase = new AppBaseModel();
        selectAppBase.setAppmark(appmark);
        AppBaseModel appBaseModel = appBaseMapper.selectOne(selectAppBase);
        if(appBaseModel==null || "".equals(appBaseModel.getAppid())){
            logger.error("EOA-TST-交易查询失败，无appmark相关配置");
            result.setStatus("1");
            result.setMessage("交易查询失败，无appmark相关配置");
            return result;
        }
        // 根据orderid或tradeid查询交易记录
        Wrapper<TransactionModel> wrapper = new EntityWrapper<TransactionModel>();
        String orderId = req.getOrderid();
        if(StringUtils.isNotEmpty(orderId)){
            wrapper.eq("orderid",orderId);
        }
        String tradeid = req.getTradeid();
        if(StringUtils.isNotEmpty(tradeid)){
            wrapper.eq("tradeid",tradeid);
        }
        Integer appid = appBaseModel.getAppid();
        wrapper.eq("appid",appid+"appid");
        // 查询交易记录表
        List<TransactionModel> transactionList = transactionMapper.selectList(wrapper);
//        if(transactionList.size() == 0){
//            logger.error("EOA-TST-交易查询失败，交易不存在");
//            result.setStatus("1");
//            result.setMessage("交易不存在");
//            return result;
//        }
        result.setStatus("0");
        result.setMessage("交易提交成功");
        result.setContext(transactionList.get(0));
		return result;
	}



}
